package com.hboxs.asl.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by hboxs001 on 2015/11/17.
 */
public class DateUtils {

    public static Date long2Date(Long timestamp) throws ParseException {
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String str = format.format(timestamp);
        Date date = format.parse(str);
        return date;
    }

    public static int getDiffYears(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
        if (a.get(Calendar.DAY_OF_YEAR) < b.get(Calendar.DAY_OF_YEAR)) {
            diff++;
        }
        return diff;
    }

    public static Date string2Date(String str) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date stringToDate(String str) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static int getDiffMonth(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
        int month = b.get(Calendar.MONTH) - a.get(Calendar.MONTH);
        int day = b.get(Calendar.DAY_OF_MONTH) - a.get(Calendar.DAY_OF_MONTH);
        if (day > 0) {
            month++;
        }
        if (diff > 0) {
            month = month + (diff * 12);
        }
        if (month <= 0) {
            month = 1;
        }
        return month;
    }

    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.CHINESE);
        cal.setTime(date);
        return cal;
    }

    public static String dateStr(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static String date2Str(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static String dateToStr(Date date){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
    }

    public static String dateToHMS(Date date){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, past);
        Date today = calendar.getTime();
        return today;
    }

    public static Date starOftDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date endOfDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /*
    * 毫秒转化
    */
    public static String formatTime(long ms) {

        //TODO 一秒1000毫秒
        Integer ss = 1000;
        //TODO 一分60秒
        Integer mi = ss * 60;
        //TODO 一小时60分钟
        Integer hh = mi * 60;
        //TODO 一天24小时
        Integer dd = hh * 24;

        //TODO 获取天数
        Long day = ms / dd;
        //TODO 获取小时
        Long hour = (ms - day * dd) / hh;
        //TODO 获取分钟
        Long minute = (ms - day * dd - hour * hh) / mi;
        //TODO 获取秒数
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        return sb.toString();
    }

}
